<?php
//ref: http://devzone.zend.com/article/3450 decorators etc.

	class Default_Form_User extends Default_Form_Common
	{

		public function init() {
			parent::init();
		}

	    public function login()
	    {

			$email = $this->createElement('text', 'email');
			$email->setLabel('E-mail')
	               	->setRequired(true)
	               	->addValidator('stringLength', false, array(3, 120))
	               	->addValidator('EmailAddress')
	               	->addFilter('StripTags')
	               	->addFilter('StringToLower')
	               	->addFilter('StringTrim')
	               	->setAttrib('size', '40')
	               	;


			$password = $this->createElement('password', 'password');
			$password->setLabel('Password')
	               	->setRequired(true)
	               	->addValidator('stringLength', false, array(3, 20))
	               	->addFilter('StripTags')
	               	->addFilter('StringTrim')
	               	;

			$this
			     ->addElement($email)
			     ->addElement($password)
			     ;

		}

	    public function forgotPassword()
	    {

			$email = $this->createElement('text', 'email');
			$email->setLabel('E-mail')
	               	->setRequired(true)
	               	->addValidator('stringLength', false, array(3, 120))
	               	->addValidator('EmailAddress')
	               	->addFilter('StripTags')
	               	->addFilter('StringToLower')
	               	->addFilter('StringTrim')
	               	;


			$this
			     ->addElement($email)
			     ;

		}

	    public function signup($signupUserType='')
	    {

			$firstname = $this->createElement('text', 'firstname');
			$firstname->setLabel('firstname')
	               	->setRequired(true)
	               	->addValidator('stringLength', false, array(2, 120))
	               	->addFilter('StripTags')
	               	->addFilter('StringTrim')
                    ->setDescription('As written on your ID card')
	               	;

			$lastname = $this->createElement('text', 'lastname');
			$lastname->setLabel('lastname')
	               	->setRequired(true)
	               	->addValidator('stringLength', false, array(2, 120))
	               	->addFilter('StripTags')
	               	->addFilter('StringTrim')
	               	->setAttrib('size', '40')
                    ->setDescription('As written on your ID card')
                ;

/*
			$nationalIdNumber = $this->createElement('text', 'national_id_number');
			$nationalIdNumber->setLabel('TC Kimlik No')
	               	->setRequired(true)
	               	->addValidator('stringLength', 11, 11)
	               	->addValidator('digits')
	               	->addFilter('StripTags')
	               	->setAttrib('maxlength', 11)
	               	//->addFilter('StringTrim')
	               	;

*/

      $uniqEmailValidator = new Zend_Validate_Db_NoRecordExists( array('table' => 'users', 'field' => 'usermail') );
      $uniqEmailValidator->setMessage('duplicate e-mail address');

			$email = $this->createElement('text', 'email');
			$email->setLabel('E-mail')
	               	->setRequired(true)
	               	->addValidator('stringLength', false, array(3, 120))
	               	->addValidator('EmailAddress')

	               	//->addValidator(new My_Validate_UniqueEmail()) //
	               	->addValidator($uniqEmailValidator)
	               	//->setDescription('')
	               	->addFilter('StripTags')
	               	->addFilter('StringToLower')
	               	->addFilter('StringTrim')
	               	->setAttrib('size', '50')
	               	;
            //if ($signupUserType != 'institution') {
            if (!in_array($signupUserType, array('institution', 'incoming-staff'))) {
				$email->addValidator(new My_Validate_AllowedEmailDomain());
			}

	        $model = new Default_Model_DbTable_Gender();
	        $genderOptions = $model->getGendersAsPairs();
			$gender = new Zend_Form_Element_Select('gender');
			$gender->setLabel('gender')
			         ->setRequired(true)
			         ->addMultiOption('', 'select-label')
			         ->addMultiOptions($genderOptions)
			         ;

          	//if ($signupUserType == 'institution') {
            if (in_array($signupUserType, array('institution', 'incoming-staff'))) {

		        $universityObj = new Default_Model_DbTable_University();
		        $universities = $universityObj->getUniversitiesErasmusCodesAsPairs();
				$university = new Zend_Form_Element_Select('university_id');
				$university->setLabel('Your Institution')
				         ->setRequired(true)
				         ->addMultiOption('', 'select-label')
				         ->addMultiOptions($universities)
				         ->setDescription('If you cannot find your institution please contact us!')
				         ;
				$this->addElement($university);
	      	}

            /*//gerek yok
          	if ($signupUserType == 'outgoing-staff') {
                $userType = $this->createElement('hidden', 'userType')->setValue($signupUserType);
				$this->addElement($userType);
	      	}*/



			$password = $this->createElement('password', 'password');
			$password->setLabel('Password')
	               	->setRequired(true)
	               	->addValidator('stringLength', false, array(3, 20))
	               	->addFilter('StripTags')
	               	->addFilter('StringTrim')
	               	;


			$passwordConfirm = $this->createElement('password', 'passwordConfirm');
			$passwordConfirm->setLabel('confirm-password')
	               	->setRequired(true)
	               	->addValidator('stringLength', false, array(3, 20))
	               	->addValidator(new My_Validate_PasswordMatch())
	               	//->addValidator('CheckPasswordsMatch', false, 'password')
	               	->addFilter('StripTags')
	               	->addFilter('StringTrim')
	               	;


		     $this->addElement($firstname)
			     ->addElement($lastname)
			     ->addElement($email)
			     ->addElement($gender)
			     //->addElement($nationalIdNumber)
			     ->addElement($password)
			     ->addElement($passwordConfirm)
			     ;


	    }

		public function add()
	    {

	    	$this->signup();//use base signup form
	    	$this->getElement('gender')->setRequired(false);
	    	//$this->getElement('email')->removeValidator('My_Validate_AllowedEmailDomain');//because admin can add user with any email domain

	    	$this->roleBox();
	    	$this->userTypeBox();
	    	//$this->getElement('role')->setOrder(3);

	    	$this->userGroupBox();

			$isActive = $this->createElement('checkbox', 'isActive');
			$isActive->setLabel('is-active');

			$sendActivationCode = $this->createElement('checkbox', 'sendActivationCode');
			$sendActivationCode->setLabel('user-send-activation-code');


			$this->addElement($isActive)
			     ->addElement($sendActivationCode)
			     ;



	    }

		public function edit()
	    {

			$editId = $this->createElement('hidden', 'editId');

			$firstname = $this->createElement('text', 'firstname');
			$firstname->setLabel('firstname')
	               	->setRequired(true)
	               	->addValidator('stringLength', false, array(2, 120))
	               	->addFilter('StripTags')
	               	->setAttrib('size', '40')
	               	->addFilter('StringTrim')
	               	;

			$lastname = $this->createElement('text', 'lastname');
			$lastname->setLabel('lastname')
	               	->setRequired(true)
	               	->addValidator('stringLength', false, array(2, 120))
	               	->addFilter('StripTags')
	               	->setAttrib('size', '40')
	               	->addFilter('StringTrim')
	               	;


			$email = $this->createElement('text', 'email');
			$email->setLabel('E-mail')
	               	->setRequired(true)
	               	->addValidator('stringLength', false, array(6, 120))
	               	->addValidator('EmailAddress')
	               	->addFilter('StripTags')
	               	->addFilter('StringToLower')
	               	->setAttrib('size', '40')
	               	->addFilter('StringTrim')
	               	;


			if (CLIENT_NAME == 'demo') {
				$email->setDescription('disabled-in-demo-mode');
				$email->setAttrib('disabled', 'disabled');
			}
/*
	        $model = new Default_Model_DbTable_Role();
	        $roles = $model->getRolesAsPairs();
			$role = new Zend_Form_Element_Select('role');
			$role->setLabel('role')
			         ->setRequired(true)
			         ->addMultiOption('', 'select-label')
			         ->addMultiOptions($roles)
			         ;
*/

			$password = $this->createElement('password', 'password');
			$password->setLabel('Password')
	               	//->setRequired(true)
	               	->addValidator('stringLength', false, array(3, 120))
	               	->addFilter('StripTags')
	               	->addFilter('StringTrim')
	               	;


			$passwordConfirm = $this->createElement('password', 'passwordConfirm');
			$passwordConfirm->setLabel('confirm-password')
	               	//->setRequired(true)
	               	->addValidator('stringLength', false, array(3, 120))
	               	->addValidator(new My_Validate_PasswordMatch())
	               	->addFilter('StripTags')
	               	->addFilter('StringTrim')
	               	;

			$isActive = $this->createElement('checkbox', 'isActive');
			$isActive->setLabel('is-active');


			$isDeleted = $this->createElement('checkbox', 'isDeleted');
			$isDeleted->setLabel('is-deleted');

			$submit = $this->createElement('submit', 'login');
			$submit->setLabel('buttons:update')
               	/*->setDecorators(array(
                   array('ViewHelper',
                   array('helper' => 'formSubmit'))
               ))*/;



			$this->addElement($editId)
				 ->addElement($firstname)
			     ->addElement($lastname)
			     ->addElement($email)
			     //->addElement($role)
			     ;
			$this->roleBox();
	    	$this->userTypeBox();
	    	$this->userGroupBox();

			$this
			     ->addElement($password)
			     ->addElement($passwordConfirm)
			     ->addElement($isActive)
			     ->addElement($isDeleted)
			     ->addElement($submit);

			/*
			$this->setElementDecorators(array(
			    'ViewHelper',
			    'Errors',
			    array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
			    array('Label', array('tag' => 'td'),
			    array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
			)));
			*/


	    }

	    public function activate()
	    {
			$activationCode = $this->createElement('text', 'code');
			$activationCode->setLabel('Activation Code')
	               	->setRequired(true)
	               	->addFilter('StripTags')
	               	->addFilter('StringTrim')
	               	;

			$submit = $this->createElement('submit', 'activate');
			$submit->setLabel('Activate my membership');


			$this->addElement($activationCode)
			     ->addElement($submit);

	    }


	    public function changePassword()
	    {


			$passwordCurrent = $this->createElement('password', 'passwordCurrent');
			$passwordCurrent->setLabel('current-password')
	               	->setRequired(true)
	               	->addValidator('stringLength', false, array(3, 20))
	               	->addFilter('StripTags')
	               	->addFilter('StringTrim')
	               	;


			$password = $this->createElement('password', 'password');
			$password->setLabel('Password')
	               	->setRequired(true)
	               	->addValidator('stringLength', false, array(3, 20))
	               	->addFilter('StripTags')
	               	->addFilter('StringTrim')
	               	;

			$passwordConfirm = $this->createElement('password', 'passwordConfirm');
			$passwordConfirm->setLabel('confirm-password')
	               	->setRequired(true)
	               	->addValidator('stringLength', false, array(3, 20))
	               	->addValidator(new My_Validate_PasswordMatch())
	               	->addFilter('StripTags')
	               	->addFilter('StringTrim')
	               	;



			$this
			     ->addElement($passwordCurrent)
			     ->addElement($password)
			     ->addElement($passwordConfirm)
			     ;

		}



	    public function roleBox()
	    {



	        $model = new Default_Model_DbTable_Role();
	        $roles = $model->getRolesAsPairs();
			$role = new Zend_Form_Element_Select('role');
			$role->setLabel('roles:Role')
			         ->setRequired(false)
			         ->addMultiOption('', 'select-label')
			         ->addMultiOptions($roles)
			         ;
			if (CLIENT_NAME == 'demo') {
				$role->setDescription('disabled-in-demo-mode');
				$role->setAttrib('disabled', 'disabled');
			}

			$this->addElement($role);

		}

	    public function userTypeBox()
	    {



	        $model = new Default_Model_DbTable_User();
	        $userTypes = $model->getUserTypes();
			$usertype = new Zend_Form_Element_Select('usertype');
			$usertype->setLabel('users:user-type')
			         ->setRequired(true)
			         ->addMultiOption('', 'select-label')
			         ->addMultiOptions($userTypes)
			         ;

			$this->addElement($usertype);

		}


	    public function userGroupBox()
	    {




	        $model = new Default_Model_DbTable_User();
	        $usergroups = $model->getUserGroups();

			$usergroup = new Zend_Form_Element_Select('usergroup');
			$usergroup->setLabel('user-group')
			         ->setRequired(false)
			         ->addMultiOption('', 'select-label')
			         ->addMultiOptions($usergroups)
			         ;
			if (CLIENT_NAME == 'demo') {
				$usergroup->setDescription('disabled-in-demo-mode');
				$usergroup->setAttrib('disabled', 'disabled');
			}

			$this->addElement($usergroup);

		}

	    public function confirmTermsBox()
	    {

            if (CLIENT_NAME == 'ankara') {
                $confirmTerms = $this->createElement('checkbox', 'confirm_terms_conditions');
                $confirmTerms->setLabel('Agree to the Terms of Service')
                            //->addValidator(new Zend_Validate_NotEmpty(), true, array( 'messages' => array('isEmpty'=>'You must agree to the terms')))
                            ->setUncheckedValue('')->setRequired(true)
                            ->setDescription('confirm-terms-conditions-note')
                            ->getDecorator('Description')->setOption('escape', false)

                ;

                $confirmTerms->getDecorator('Label')->setOption('escape', false);
                $this->addElement($confirmTerms);
            }


		}



	}
